<?php

class Article
{	
    public $art_id;
    public $art_category_id;
	public $art_content;
	public $art_title;
	public $art_url_photo;
	public $art_date;
  
    # Constructeur   
    public function __construct($id='')
    {
        if($id != '') 
        {
            $this->art_id = $id;
		
            $this->load();
        }
    }
    
	# Méthode pour récupérer l'ensemble des articles
    public static function getAll() 
    {    
    		$table=array();
    		$db = Database::getInstance();
			$sql = 'SELECT *
                FROM blog_article';
				
            foreach($db->fetch($sql) as $result)
            {
            	$table[] = new Article($result['art_id']);
            } 
            
            return $table;            
    }

	
	
    # Chargement depuis la BDD
    public function load() 
    {
        if(isset($this->art_id))
       {
            $db = Database::getInstance();
            $sql = 'SELECT *                    
                    FROM blog_article
                    WHERE art_id = "'.$this->art_id.'"';
            if($result = $db->fetch($sql)) 
            {
                  # On remplit l’objet grâce aux attributs récupérés
				  $this->art_id = $result[0]['art_id'];
				  $this->art_category_id = $result[0]['art_category_id'];
                  $this->art_title = $result[0]['art_title'];
                  $this->art_content = $result[0]['art_content'];
                  $this->art_url_photo = $result[0]['art_url_photo'];
                  $this->art_date = $result[0]['art_date'];
                  return true;
            }
            return false;
        }
    }
	
		
	#Insertion dans la BDD
	public function insert() 
    {       
            $db = Database::getInstance();
            $sql = "INSERT INTO blog_article (art_title, art_content, art_url_photo, art_date, art_category_id) 
					VALUES ('".$this->art_title."', '".$this->art_content."', '".$this->art_url_photo."', '".date('Y-m-d')."', '".$this->art_category_id."' )";
			$db->exec($sql);
			
			return true;
    }
	
	public function getArticleById($id)
	{
		$db = Database::getInstance();
		$sql = 'SELECT *                    
                FROM blog_article
                WHERE art_id = "'.$id.'"';
		$db->exec($sql);
		$result = $db->fetch($sql);
		
		return $result;
	}
	
	public function delete($id)
	{
		$db = Database::getInstance();
        $sql = "DELETE FROM blog_article WHERE art_id =".$id;
		$db->exec($sql);
		return true;
	}
	
	
    # Méthode surchargée pour l'affichage de l'objet : echo $obj;
    public function __toString() 
    {
        return $this->name;
    }
	
	 public static function insertionArticle(){
	 
		echo 'TOUT VA BIEN';
	 }
	
}

?>